package com.itita.weibo.manager;

import java.io.Serializable;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import weibo4andriod.Comment;
import weibo4andriod.Count;
import weibo4andriod.DirectMessage;
import weibo4andriod.Paging;
import weibo4andriod.Status;
import weibo4andriod.User;
import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;
import weibo4andriod.http.AccessToken;
import weibo4andriod.http.ImageItem;
import weibo4andriod.http.RequestToken;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.itita.weibo.model.Task;
import com.itita.weibo.ui.AboutActivity;
import com.itita.weibo.ui.AddCountctivity;
import com.itita.weibo.ui.ListAllFriendsActivity;
import com.itita.weibo.ui.ListMyFansActivity;
import com.itita.weibo.ui.ListMyFavoriteActivity;
import com.itita.weibo.ui.ListMyWeibosActivity;
import com.itita.weibo.ui.ListRcemmendInfoActivity;
import com.itita.weibo.ui.LoginActivity;
import com.itita.weibo.ui.MessageActivity;
import com.itita.weibo.ui.MyListActivity;
import com.itita.weibo.ui.NewWeiboActivity;
import com.itita.weibo.ui.R;
import com.itita.weibo.ui.RecommendCurrAppActivity;
import com.itita.weibo.ui.SendSuggestionToAuthorActivity;
import com.itita.weibo.ui.UserInfoActivity;
import com.itita.weibo.ui.ViewWeiboActivity;
import com.itita.weibo.util.NetUtil;
import com.itita.weibo.util.WeiboUtil;

/**
 * 系统调度模块
 * @author 焦明 2010-5-4 13:00
 *
 */
public final class MainService extends Service implements Runnable {
    
 
	public static boolean isrun = false;
    
	public static boolean isLogined = false;
    
//	public static boolean isWillSave = false;
	//当前用登陆的用户
	public static User currentUser = null;
    
	// 全局静态引用
	public static MainService mainService;
   
	//是否已经验证
	public static boolean isVerifySucc = false;
	
	// 是否自动登录
	public static boolean isAutoLogin = false; //是否自动登录
  
	//是否将数据离线到笨地
	 public static boolean isWillSaved = false;
	 
//	public final NetUtil netReceiver = new NetUtil();
  
	// 定义微博处理对象
//	public Weibo weibo = new Weibo();
	 
	public Weibo weibo ;
//	public Weibo weibo = OAuthConstant.getInstance().getWeibo();
	
	// 定义错误信息
	public int weiboerror = 100;
	
	public static  String tempUsername = null;
	// 保存所有任务对象
	private static final ArrayList<Task> allTask = new ArrayList<Task>();
	
	// 保存所有 Activity
	private static final ArrayList<IWeiboActivity> allActivity = new ArrayList<IWeiboActivity>();

	// 保存所有的用户头像
//	public static final Map<Integer, SoftReference<Bitmap>> allIcon = new HashMap<Integer, SoftReference<Bitmap>>();
	public static final Map<Integer, Bitmap>   allIcon = new HashMap<Integer, Bitmap>();
	
	// 用来存放图片的容器
//	public static final Map<Long, SoftReference<Bitmap>> allImage = new HashMap<Long, SoftReference<Bitmap>>();
	private  static final Map<Long, Bitmap> allImage = new HashMap<Long, Bitmap>();
    
	//用户任务处理得线程池
	//public static final ArrayList<TaskThread> taskThreads = new ArrayList<MainService.TaskThread>();
	
	private WeiboManager weiboManager = new WeiboManager();
	
	/**
	 * 从图片容器中获取图片
	 * @param statusId
	 * @return
	 */
	public static final synchronized Bitmap getAllImage(long statusId){
		synchronized (allImage) {
			return allImage.get(statusId);
		}
	}
	
	public static final synchronized Bitmap putAllImage(long statusId, Bitmap bitmap){
		synchronized (allImage) {
			return allImage.put(statusId, bitmap);
		}
	}
	/**
	 * 向任务队列中添加一个任务
	 * @param task 任务
	 */
	public static final synchronized void newTask(Task task) {
		allTask.add(task);
	}
    
	/**
	 * 向任务队列中添加一个任务
	 * @param task 任务
	 */
	public static final synchronized void newTaskToTaskQueneHead(Task task) {
		allTask.add(0, task);
	}

	
	public MainService() {
		mainService = this;
	}
     
	
	/**
	 * 根据Activity获得该Activity的实例
	 * 
	 * @param name
	 *            Activity的名字
	 * @return 返回该Activity的实例
	 */
	public static IWeiboActivity getActivityByName(String name) {
		IWeiboActivity ia = null;
		for (IWeiboActivity ac : allActivity) {
			if (ac.getClass().getName().indexOf(name) >= 0) {
				ia = ac;
			}
		}
		return ia;
	}

	public void onCreate() {
		////System.out.println("-----MainService--onCreate----------");
		super.onCreate();
		mainService = this;
		Thread t = new Thread(this);
		t.start();
		
//		// 添加网络状态变化的广播接收器
//		this.registerReceiver(netReceiver, new IntentFilter(
//				"android.net.conn.CONNECTIVITY_CHANGE"));
	}

	
	public void run() {
		// 获取所有线程堆栈
		// Map<Thread, StackTraceElement[]> threads =
		// Thread.getAllStackTraces();

		if (weibo == null) {
			weibo = new Weibo();
		}
		Task lasttask = null;
		while (isrun) {
			Log.d("core Logic", "+++run++++");
			synchronized (allTask) {
				if (allTask.size() > 0) { 
					// 取任务
					lasttask = allTask.get(0);
					// 执行任务
					doTask(lasttask);
				}
				
			}
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				
			}
		}
	}
	
	// 执行任务
	public void doTask(Task task) {

		////System.out.println("当前的任务总数 为:" + allTask.size());
		Message message = mHandler.obtainMessage();
		try {
			////System.out.println("任务ID：" + task.getTaskID());
			switch (task.getTaskID()) {
			
			case Task.TASK_USER_LOGIN:// 用户登录
				String result = "";
				RequestToken requestToken; 
//				System.setProperty("weibo4j.oauth.consumerKey", "1911463794");
//				System.setProperty("weibo4j.oauth.consumerSecret", "ac42ddaa954031a5cbc0503a36732bad");
				System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
				System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
				weibo = new Weibo();
//				weibo = OAuthConstant.getInstance().getWeibo();
				try { 
					 				
					  if(!MainService.isAutoLogin){ //不是自动登录
						 requestToken = weibo.getOAuthRequestToken();
						 String username =  (String)task.getTaskParam().get("user");
						 String password = 	(String) task.getTaskParam().get("pass");
//						 String vcode = weibo.getAuthorizationURL(requestToken.getToken(), username, password);
							String vcode = weibo.getAuthorizationURL(requestToken
									.getToken(),
									username,
									password);
							Log.d("rctoken", "获取 Vcode" + vcode);
						 // 使用verifier_code和requestToken获取Access_Token
					     AccessToken accessToken = requestToken.getAccessToken(vcode);
					     //---->
					     weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
						 //保存用的登陆数据
					     try {
					    	 currentUser = new User();
					    	 currentUser.setUsername(username);
					    	 currentUser.setPassword(password);
					    	 currentUser.setId(accessToken.getUserId());
					    	 currentUser.setLogin(true);
					    	 currentUser.setName(null);
					    	 currentUser.setAccessToken(accessToken.getToken());
					    	 currentUser.setAccessTokenSecret(accessToken.getTokenSecret());
//					    	 weiboManager.addUser(this, currentUser);
					    	 
					    	 HashMap param = new HashMap();
					    	 param.put("uid", String.valueOf(accessToken.getUserId()));
							 Task ts1 = new Task(Task.TASK_SAVE_LOIGINING_USER_BASE_INFO, param);
					         MainService.addNewTask(ts1);
					         
					     } catch (Exception e) {
						   Log.e("用户存储", "存储失败");  
						   e.printStackTrace();
					     }
					  }else{
						  weibo.setUserId(String.valueOf(currentUser.getId()));
						  weibo.setToken(currentUser.getAccessToken(), currentUser.getAccessTokenSecret()); 
						  Log.i("自动登陆提示", "登陆成功");
					  }
					  isVerifySucc = true;
					  result = "登陆成功";
				} catch (Exception e) {
					Log.e("登陆发生错误", e.getMessage());
					result = "用户名或者密码错误";
					isVerifySucc = false;
					e.printStackTrace();
				} 
				message.obj = result; // 保存登录结果
				break;
				
			case Task.TASK_MODIFY_USER_LOGIN:// 用户登录
				String result2 = "";
				weibo = null;
				weibo = new Weibo();
				RequestToken requestToken21 = null; 
				System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
				System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
				
				try {
					
					requestToken21 =  requestToken = weibo.getOAuthRequestToken();	
					String username =  (String)task.getTaskParam().get("user");
					String password = 	(String) task.getTaskParam().get("pass");
					String vcode = weibo.getAuthorizationURL(requestToken21.getToken(), username, password);
					 // 使用verifier_code和requestToken获取Access_Token
				     AccessToken accessToken = requestToken21.getAccessToken(vcode);
				     //---->
				     weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
				     MainService.currentUser = new User();
					//更换当前用户
					 currentUser = new User();
			    	 currentUser.setUsername(username);
			    	 currentUser.setPassword(password);
			    	 currentUser.setId(accessToken.getUserId());
			    	 currentUser.setLogin(true);
			    	 currentUser.setName(null);
			    	 currentUser.setAccessToken(accessToken.getToken());
			    	 currentUser.setAccessTokenSecret(accessToken.getTokenSecret());
			    	 HashMap param = new HashMap();
			    	 param.put("uid", String.valueOf(accessToken.getUserId()));
					 Task ts1 = new Task(Task.TASK_SAVE_LOIGINING_USER_BASE_INFO, param);
			         MainService.addNewTask(ts1);
//				     //保存用的登陆数据
//					try {
//						 weiboManager.addUser(this, username, password, accessToken.getUserId(), true, null, accessToken.getToken(),accessToken.getTokenSecret());
//					} catch (Exception e) {
//						Log.e("用户存储", "存储失败");  
//						e.printStackTrace();
//					}
				    //如果		
					isVerifySucc = true;
					result2 = "登陆成功";
				} catch (Exception e) {
					Log.e("登陆发生错误", e.getMessage());
					result2 = "用户名或者密码错误";
					isVerifySucc = false;
					e.printStackTrace();
				} 
				message.obj = result2; // 保存登录结果
				break;
             
			case Task.TASK_SAVE_LOIGINING_USER_BASE_INFO://获取登陆用户信息
			    User user = null;
				try {
					user = weibo.showUser((String)task.getTaskParam().get("uid"));
				} catch (Exception e6) {
					// TODO Auto-generated catch block
					e6.printStackTrace();
				}
				if(user != null){
					 try {
						 currentUser.setName(user.getName());
				     } catch (Exception e) {
					   Log.e("用户存储", "存储失败");  
					   e.printStackTrace();
				     }
				}
				 weiboManager.addUser(this, currentUser);
			    break;
				
			  case Task.TASK_GET_USER_INFO:  //获取用户详细信息
				 
				 try {
					message.obj = weibo.showUser(String.valueOf(currentUser.getId()));
				 } catch (Exception e5) {
					message.obj  = null;
					e5.printStackTrace();
				 }
				 break;
				    
				    
			case Task.TASK_GET_USER_HOMETIMEINLINE:// 获取用户首页微博信息
				try {
					if(!MainService.isAutoLogin){ 
						message.obj  = weiboManager.loadStatusesInfoFromNet((Integer) task.getTaskParam().get("nowpage"),
								     (Integer) task.getTaskParam().get("pagesize"));
					}else{//自动登录
						message.obj = weiboManager.loadStatusesInfoFromLocal(this);
					}
				} catch (Exception e1) {
					message.obj = null;
					e1.printStackTrace();
				}
				
				break;
				  
			case Task.TASK_GET_USER_HOMETIMEINLINE_REFRESH://刷新获取更多用户首页微博信息
				try {
					message.obj  = weiboManager.loadStatusesInfoFromNet((Integer)task.getTaskParam().get("nowpage"), 
							(Integer)task.getTaskParam().get("pagesize"));
				} catch (Exception e) {
					message.obj = null;
					e.printStackTrace();
				}
				break;
				
		     case Task.TASK_GET_USER_HOMETIMEINLINE_MORE://获取更多用户首页微博信息
				 try {
					  message.obj  = weiboManager.loadStatusesInfoFromNet((Integer)task.getTaskParam().get("nowpage"), 
							 (Integer)task.getTaskParam().get("pagesize"));
				  } catch (Exception e) {
					message.obj = null;
					e.printStackTrace();
				  }
				  break;
			
			 //获取推荐信息
		     case Task.TASK_GET_USER_RECOMMEND_INFO_HOME_INLINE:
		    	 
		    	 message.obj = weiboManager.loadRecommendInfoFromLocal(this, (Integer) task.getTaskParam().get("nowpage"),
					     (Integer) task.getTaskParam().get("pagesize"));
		    	 
		    	 break;
				
		     //刷新推荐信息	 
		     case Task.TASK_GET_USER_RECOMMEND_INFO_HOME_INLINE_REFRESH: //刷新推荐信息
//		    	 ArrayList<Status> recAlls = weiboManager.loadRecommendInfoFromNet(this);
		    	
				ArrayList<Status> recAlls = weiboManager.loadRecommendInfoFromNet(this, (Integer) task.getTaskParam().get("nowpage"),
				         (Integer) task.getTaskParam().get("pagesize"), (Integer) task.getTaskParam().get("basePageSize"));
		    	
		    	 if(recAlls.isEmpty()){
		    		 message.obj = null;
		    	 }else{
		    		 message.obj = recAlls;
		    	 }
		    	 break;
		       	 
		     case Task.TASK_GET_USER_RECOMMEND_INFO_HOME_INLINE_MORE: //获取更多的推荐信息
		    	 ArrayList<Status> recMoreAlls = weiboManager.loadRecommendInfoFromNet(this, (Integer) task.getTaskParam().get("nowpage"),
					         (Integer) task.getTaskParam().get("pagesize"), (Integer) task.getTaskParam().get("basePageSize"));
		    	 if(recMoreAlls.isEmpty()){
		    		 message.obj = null;
		    	 }else{
		    		 message.obj = recMoreAlls;
		    	 }
		    	 break;
		    	 
//---------------------------------------------------
		     case Task.TASK_GET_WEIBO_COMMENT_LIST: //查看微博信息的评论列表
		    	 long statusId4CommList = (Long) task.getTaskParam().get("statusId");
		    	 try {
					message.obj  = MainService.mainService.weibo.getComments(String.valueOf(statusId4CommList));
				 } catch (Exception e4) {
					 message.obj = null; 
					e4.printStackTrace();
				 }
		    	 break;
//------------------------微博中的图片--------------------------------------------------------
			  case Task.TASK_GET_WEIBO_IMAGE: //加载微博中的图片
				   //得到图片对应的UID
				   try {
					   long statusId =(Long)task.getTaskParam().get("statusId");
					   String imageUrl = (String) task.getTaskParam().get("imageUrl");
					   //加载图片并将图片放到指定的图片容器中
					   WeiboUtil.processSattusImage(statusId, imageUrl, weiboManager);
				   } catch (Exception e3) {
					  e3.printStackTrace();
				   }
				   break;
			 
			  //加载提到我的微博信息中的图片信息
			  case Task.TASK_GET_USER_WEIBO_IMAGE_FOR_METTION_ME: 
				  try {
					  long statusId =(Long)task.getTaskParam().get("statusId");
					  String imageUrl = (String) task.getTaskParam().get("imageUrl");
					  WeiboUtil.processSattusImage(statusId, imageUrl, weiboManager);
				   } catch (Exception e2) {
					  e2.printStackTrace();
				   }
				  break;
				  
			 //加载提到 推荐 信息中的图片信息
			  case Task.TASK_GET_USER_WEIBO_IMAGE_FOR_RECOMMOND_STATUS: 
				  try {
					  long statusId =(Long)task.getTaskParam().get("statusId");
					  String imageUrl = (String) task.getTaskParam().get("imageUrl");
					  WeiboUtil.processSattusImage(statusId, imageUrl, weiboManager);
				  } catch (Exception e2) {
					  e2.printStackTrace();
				  }
				  break;
				  
			  //加载提到 收藏夹信息中的图片信息
			  case Task.TASK_GET_USER_WEIBO_IMAGE_FOR_FAV_STATUS: 
				  try { 
					  long statusId =(Long)task.getTaskParam().get("statusId");
					  String imageUrl = (String) task.getTaskParam().get("imageUrl");
					  WeiboUtil.processSattusImage(statusId, imageUrl, weiboManager);
				  } catch (Exception e2) {
					  e2.printStackTrace();
				  }
				  break;
			  //加载提到 收藏夹信息中的图片信息 ---ViewWeiboActivity	  
			  case Task.TASK_GET_WEIBO_IMAGE_FOR_VIEW_WEIBO_INFO: 
				  try { 
					  long statusId =(Long)task.getTaskParam().get("statusId");
					  String imageUrl = (String) task.getTaskParam().get("imageUrl");
					  WeiboUtil.processSattusImage(statusId, imageUrl, weiboManager);
				  } catch (Exception e2) {
					  e2.printStackTrace();
				  }
				  break;
				   
			   
//---------------------------用户的头像-------------------------------------
			   //从网络获取用户的头像
			  case Task.TASK_GET_USER_IMAGE_ICON:
				 try {
					    Integer uid2=(Integer)task.getTaskParam().get("uid");
					    URL url=(URL) task.getTaskParam().get("url");
						WeiboUtil.processUserIcon(uid2, url, weiboManager);
				 } catch (Exception e1) {
						e1.printStackTrace();
				 }
				   break;
			 
			  //信息推荐--->用户头像
			  case Task.TASK_GET_USER_IMAGE_ICON_FOR_RECOMMOND_STATUS:
				  try {
					    Integer uid2=(Integer)task.getTaskParam().get("uid");
					    URL url=(URL) task.getTaskParam().get("url");
						WeiboUtil.processUserIcon(uid2, url, weiboManager);
				  } catch (Exception e1) {
						e1.printStackTrace();
				  }
				  break;
			  //提到我的微博信息---> 用户头像	  
			  case Task.TASK_GET_USER_IMAGE_ICON_FOR_METTION_ME:
				  try {
					  Integer uid2=(Integer)task.getTaskParam().get("uid");
					  URL url=(URL) task.getTaskParam().get("url");
					  WeiboUtil.processUserIcon(uid2, url, weiboManager);
				  } catch (Exception e1) {
					  e1.printStackTrace();
				  }
				  break;
				   
			   case Task.TASK_GET_USER_IMAGE_ICON_ATTENTION:
				 try {
				    Integer uid2=(Integer)task.getTaskParam().get("uid");
				    URL url=(URL) task.getTaskParam().get("url");
					WeiboUtil.processUserIcon(uid2, url, weiboManager);
				 } catch (Exception e1) {
					e1.printStackTrace();
				 }
				   break;
			   
			   //获取粉丝头像
			   case Task.TASK_GET_USER_IMAGE_ICON_FANS:
				   try {
					    Integer uid3=(Integer)task.getTaskParam().get("uid");
					    URL url=(URL) task.getTaskParam().get("url");
						WeiboUtil.processUserIcon(uid3, url, weiboManager);
					 } catch (Exception e1) {
						e1.printStackTrace();
					 }
				   break;
			   
			   
			   //登陆用户的头像   
			   case Task.TASK_GET_USER_ICON_4_USERINFO:
				   try {
					   Integer uid3=(Integer)task.getTaskParam().get("uid");
					   URL url=(URL) task.getTaskParam().get("url");
					   WeiboUtil.processUserIcon(uid3, url, weiboManager);
				   } catch (Exception e1) {
					   e1.printStackTrace();
				   }
				   
				   break;
			   
				   
			   //获取评论用户的头像	    
			   case Task.TASK_GET_COMM_USERT_ICON:
				   try {
					    Integer uid3=(Integer)task.getTaskParam().get("uid");
					    URL url=(URL) task.getTaskParam().get("url");
						WeiboUtil.processUserIcon(uid3, url, weiboManager);
					 } catch (Exception e1) {
						e1.printStackTrace();
					 }
				   break;
			   
			   //获取发私信给我的好友的头像	   
			   case Task.TASK_GET_DIRECT_MSG_USERT_ICON:
				   try {
					    Integer uid3=(Integer)task.getTaskParam().get("uid");
					    URL url=(URL) task.getTaskParam().get("url");
						WeiboUtil.processUserIcon(uid3, url, weiboManager);
					 } catch (Exception e1) {
						e1.printStackTrace();
					 }
				   break;
				   
			   case Task.TAST_GET_FANS_INFO: //获取粉丝
				   
				   Paging paging4Fans = new Paging((Integer)task.getTaskParam().get("nowpage4Fans"), (Integer)task.getTaskParam().get("pagesize4Fans"));
				   ArrayList<User> attentionFriends4Fans = (ArrayList<User>) weibo.getFollowersStatuses(paging4Fans);
				   WeiboUtil.processFans(attentionFriends4Fans);
				   message.obj = attentionFriends4Fans;
				   break;
				   
			   case Task.TAST_GET_MORE_FANS_INFO: //获取粉丝

				   Paging paging4MoreFans = new Paging((Integer)task.getTaskParam().get("nowpage4Fans"), (Integer)task.getTaskParam().get("pagesize4Fans"));
				   ArrayList<User> attentionFriends4MoreFans =( ArrayList<User>) weibo.getFollowersStatuses(paging4MoreFans);
				   WeiboUtil.processFans(attentionFriends4MoreFans);
				   
				   message.obj = attentionFriends4MoreFans;
				   break;
				   
			   case Task.TASK_ADD_MORE_STATUS_TO_LOCAL :
				   ////System.out.println("---------TASK_ADD_MORE_STATUS_TO_LOCAL--添加数据到本地任务执行()----------------");
				   ArrayList<Status> statuses = (ArrayList<Status>) task.getTaskParam().get("statuses");
					if(statuses == null || statuses.size() == 0){
						Log.d("保存的微博信息", "************statuses为空*没有可保存的微博信息***************");
					    return;
					} 
					weiboManager.saveStatusDataToLocalHomeTable(statuses, this);
				   break;
			/**
			 * 州	   
			 */
			   case Task.TASK_GET_WEIBO_USER:
					// 获取我的微博列表
					Paging p3 = new Paging((Integer) task.getTaskParam().get("nowpage"), (Integer) task.getTaskParam().get("pagesize"));// 创建分页对象
					ArrayList<Status> alls3 = (ArrayList<Status>) weibo.getUserTimeline(p3);
					Status st = alls3.get(0);
					HashMap param = new HashMap();
					param.put("uid", st.getUser().getId());
					param.put("url", st.getUser().getProfileImageURL());

					Task ts = new Task(Task.TASK_GET_USER_IMAGE_ICON, param);
					MainService.addNewTask(ts);

					message.obj = alls3;
					break;

			case Task.TASK_GET_MORE_WEIBO_USER:
				int nowpage4 = (Integer) task.getTaskParam().get("nowpage");
				int pagesize4 = (Integer) task.getTaskParam().get("pagesize");
				Paging p4 = new Paging(nowpage4, pagesize4);// 创建分页对象
				List<Status> alls4 = weibo.getUserTimeline(p4);
				Status st1 = alls4.get(0);
				HashMap param1 = new HashMap();
				param1.put("uid", st1.getUser().getId());
				param1.put("url", st1.getUser().getProfileImageURL());

				Task ts1 = new Task(Task.TASK_GET_USER_IMAGE_ICON, param1);
				MainService.addNewTask(ts1);

				message.obj = alls4;
				break;
			  case Task.TASK_NEW_WEIBO://发表普通微博信息
				  try {
					  Status ststus= weibo.updateStatus((String)task.getTaskParam().get("msg"));
					  message.obj="微博发表成功";
				  } catch (Exception e) {
					  e.printStackTrace();
					  message.obj="由于网络原因,微博发表失败";
				  }
			       break;
				   
				   
			   case Task.TASK_NEW_WEIBO_PIC://发表图片微博信息
				   try {
					   String msg = URLEncoder.encode((String)task.getTaskParam().get("msg") , "UTF-8");
					   ImageItem it=new ImageItem("pic",(byte[])task.getTaskParam().get("picdat"));
					   //发表图片微博
					   Status status = weibo.uploadStatus(msg, it);
				       message.obj="微博发表成功";
				   } catch (Exception e) {
					   message.obj="由于网络原因,微博发表失败";
				   }
			       break;
			  
			   case Task.TASK_NEW_WEIBO_GPS://发表GPS微博信息
				  try {
					//发表图片微博
					  Status statusgps = weibo.updateStatus(
							  (String)task.getTaskParam().get("msg"), 
							  ((double[])task.getTaskParam().get("gpspoint"))[0],
							  ((double[])task.getTaskParam().get("gpspoint"))[1]);
					  message.obj="微博发表成功";
				} catch (Exception e) {
					e.printStackTrace();
					message.obj="由于网络原因,微博发表失败";
				}
			      break;
			   
			    //发表反馈信息     
			 case Task.TASK_NEW_SUGGESTION_TO_AUTHOR_WEIBO:
				  try {
					  weibo.sendDirectMessage("1967935164", (String)task.getTaskParam().get("msg"));
					  message.obj="反馈意见发送成功";
				  } catch (Exception e) {
					  e.printStackTrace();
					  message.obj="由于网络原因,反馈意见发送失败";
				  }
				  break;
			      
			      
	//------------------------Message-----------------------------------	   
		  
			   case Task.TASK_GET_METION_ME:    //获取提到我的所有所有微博信息
				   try {
					   ArrayList<Status> alls=(ArrayList<Status>) weibo.getMentions();//我发表的所有评论
					   message.obj=alls;
					   //处理微博信息中的头像， 图片任务
					   WeiboUtil.processStatuses(alls,  
							   Task.TASK_GET_USER_IMAGE_ICON_FOR_METTION_ME,
							   Task.TASK_GET_USER_WEIBO_IMAGE_FOR_METTION_ME);
					  
				   } catch (Exception e) {
					   e.printStackTrace();
					   message.obj = null;
				   }
				   break;   
				      
			   case Task.TASK_GET_WEIBO_COMMENT://获取所有微博评论信息
				   try {
					   ArrayList<Comment> allc=(ArrayList<Comment>) weibo.getCommentsToMe();//我发表的所有评论
					   message.obj=allc;
					   WeiboUtil.processComment(allc);
					} catch (Exception e) {
						message.obj = null;
						 e.printStackTrace();
					}
				   break;
				    
			   case Task.TASK_GET_WEIBO_MESSAGE://获取所有私信
				   try {
					ArrayList<DirectMessage> alldc = (ArrayList<DirectMessage>) weibo.getDirectMessages();//我所有私信
					message.obj=alldc;
					WeiboUtil.processSendMessageDirect(alldc);
				} catch (Exception e1) {
					message.obj = null;
					e1.printStackTrace();
				} 
				   break;
				   
			       
			   case Task.TASK_NEW_WEIBO_COMMENT://发表微博评论
				   try {
					if(task.getTaskParam().get("news")!=null && (Boolean)task.getTaskParam().get("news")){ //同时发布一条微博
						   weibo.updateStatus((String)task.getTaskParam().get("msg"));
					   }
					   //发布评论
					  weibo.updateComment((String)task.getTaskParam().get("msg"),
							   (String)task.getTaskParam().get("id"),
							   null);
					  message.obj="微博评论发表成功";
				} catch (Exception e1) {
					 message.obj="由于网络原因,微博评论发表失败";
					e1.printStackTrace();
				}
				  
				   break;  
				   
			   case Task.TASK_REPOST_WEIBO://转发微博
				   try {
					   if(task.getTaskParam().get("news")!=null && (Boolean)task.getTaskParam().get("news")){ //同时发布一条微博
//						   weibo.updateComment(comment, id, cid)((String)task.getTaskParam().get("msg"));
						   //发布评论
						  weibo.updateComment((String)task.getTaskParam().get("msg"),
								   (String)task.getTaskParam().get("id"),
								   null);
					    }
					   //发布评论
					   weibo.repost( (String)task.getTaskParam().get("id"),(String)task.getTaskParam().get("msg")
							    );
					   message.obj="转发微博成功";
				   } catch (Exception e1) {
					   message.obj="由于网络原因,微博评论转发失败";
					   e1.printStackTrace();
				   }
				   
				   break; 
				   
			   case Task.TASK_REFRESH_WEIBO://刷新 微博
				   try {
					  long statusId = (Long) task.getTaskParam().get("statusId");
					  ArrayList<Count> counts =  (ArrayList<Count>) weibo.getCounts(String.valueOf(statusId));
					  message.obj= counts;
				   } catch (Exception e1) {
					   message.obj= null;
					   e1.printStackTrace();
				   }
				   
				   break;  
				   
			   case Task.TASK_FAV_WEIBO://收藏微博
				   try {  
					   long statusId = (Long) task.getTaskParam().get("statusId");
					   Status status= weibo.createFavorite(statusId); 
					   message.obj= status;
				   } catch (Exception e1) {
					   message.obj= null;
					   e1.printStackTrace();
				   }
				   
				   break;  
					
			 case Task.TASK_DEL_ACCOUNT: //删除用户
				try {
					String username = (String) task.getTaskParam().get("username");
					weiboManager.delUser(this, username);
					message.obj = "删除账号成功";
				} catch (Exception e1) {
					message.obj = null;
					e1.printStackTrace();
				}
					 break;
					   
				  				   
				   
				   
					//树朾添加------------------------------------------------------------------
					/*
					 *添加取消关注任务
					 */
			case Task.TASK_ATTEND_CANCEL: // 取消关注
				////System.out.println("开始执行取消关注任务：");

				message.obj = task.getTaskParam();

				@SuppressWarnings("unchecked")
				HashMap<String, Object> ctaskParam = (HashMap<String, Object>) task
						.getTaskParam();

				String cuserId = (String) ctaskParam.get("userId");

				/* User user = */
				weibo.destroyFriendship(cuserId);// 取消关注用户

				break;
			/*
			 * 添加恢复关注任务
			 */
			case Task.TASK_ATTEND_RESTORE: // 恢复关注
				////System.out.println("开始执行恢复关注任务：");

				message.obj = task.getTaskParam();

				@SuppressWarnings("unchecked")
				HashMap<String, Object> rtaskParam = (HashMap<String, Object>) task
						.getTaskParam();

				String ruserId = (String) rtaskParam.get("userId");
				/* User user = */
				weibo.createFriendship(ruserId);// 恢复关注用户
				break;
			
		/**
		 * 粉丝关注
		 */
			case Task.TASK_FANS_CANCEL: // 取消关注
				////System.out.println("开始执行取消关注任务：--------------");

				message.obj = task.getTaskParam();

				@SuppressWarnings("unchecked")
				HashMap<String, Object> ctaskPara1 = (HashMap<String, Object>) task
						.getTaskParam();

				String cuserId1 = (String) ctaskPara1.get("userId");

				/* User user = */
				weibo.destroyFriendship(cuserId1);// 取消关注用户

				break;
			/*
			 * 添加恢复关注任务
			 */
			case Task.TASK_FANS_RESTORE: // 恢复关注
				////System.out.println("开始执行恢复关注任务：----------------");

				message.obj = task.getTaskParam();

				@SuppressWarnings("unchecked")
				HashMap<String, Object> rtaskParam1 = (HashMap<String, Object>) task
						.getTaskParam();

				String ruserId1 = (String) rtaskParam1.get("userId");
				/* User user = */
				weibo.createFriendship(ruserId1);// 恢复关注用户
				break;
			/*
			 * 添加获取用户收藏
			 */ 
			case Task.TASK_GET_USER_FAVORITE: // 获取用户收藏
				try {
					ArrayList<Status>  falls = (ArrayList<Status>) weibo.getFavorites();
					////处理微博信息中的头像， 图片任务
					WeiboUtil.processStatuses(
							falls, 
							Task.TASK_GET_USER_IMAGE_ICON_FAVORITE,
							Task.TASK_GET_USER_WEIBO_IMAGE_FOR_FAV_STATUS);
					message.obj = falls;
				} catch (WeiboException e) {
					message.obj = null;
					e.printStackTrace();
				}
				break;
				
				
			case Task.TASK_GET_USER_IMAGE_ICON_FAVORITE:
				   
				 //得到头像对应的UID
			    Integer uid4=(Integer)task.getTaskParam().get("uid");
			    //首先从缓存中获取数据
			    Bitmap bitmap4 = weiboManager.getMyBitmap(String.valueOf(uid4));
                if(bitmap4 != null){
//                	allIcon.put(uid4, new SoftReference<Bitmap>(bitmap4));
                	allIcon.put(uid4,bitmap4);
                }else{
                	URL url=(URL) task.getTaskParam().get("url");
     			    BitmapDrawable bitmapDrawable =WeiboUtil.getImageFromURL(url);
     			   //添加到集合
//     			    allIcon.put(uid4, new SoftReference<Bitmap>(bitmapDrawable.getBitmap()));
     			    allIcon.put(uid4, bitmapDrawable.getBitmap());
     			   
     			    final HashMap taskparamMap2 = new HashMap();
				    taskparamMap2.put("imageId", String.valueOf(uid4));
				    taskparamMap2.put("imageResource", bitmapDrawable.getBitmap());
				    Task saveImageTask2 = new Task(Task.TASK_SAVE_IMAGE_TO_SD_CARD, taskparamMap2);
				    MainService.addNewTask(saveImageTask2);
                }
			  
			   break;
					
				 
			   case Task.TASK_SAVE_IMAGE_TO_SD_CARD:
				   ////System.out.println("00000000存储图片任务00000000");
				   String imageId =(String)task.getTaskParam().get("imageId");
				   Bitmap imageResource=(Bitmap)task.getTaskParam().get("imageResource");
				   weiboManager.saveMyBitmap(imageId, imageResource);
				   break;
			   case Task.TAST_GET_ATTENTION_INFO: //获取好像有的关注信息 
				   ////System.out.println("--获取好友有的关注信息 --");
				   int nowpage4Attention=(Integer)task.getTaskParam().get("nowpage4Attention");
				   int pagesize4Attention=(Integer)task.getTaskParam().get("pagesize4Attention");
				   ////System.out.println("nowpage4Attention:" + nowpage4Attention);
				   ////System.out.println("pagesize4Attention:" + pagesize4Attention);
				   
				   Paging paging = new Paging(nowpage4Attention, pagesize4Attention);
				   List<User> attentionFriends=weibo.getFriendsStatuses(paging);
				   message.obj = attentionFriends;
				   break;
			   case Task.TAST_GET_MORE_ATTENTION_INFO : //获取更多的关注信息
				   int nowpage4AttentionSec=(Integer)task.getTaskParam().get("nowpage4Attention");
				   int pagesize4AttentionSec=(Integer)task.getTaskParam().get("pagesize4Attention");
				   Paging pagingSec = new Paging(nowpage4AttentionSec, pagesize4AttentionSec);
				   List<User> attentionFriendsSec=weibo.getFriendsStatuses(pagingSec);
				   message.obj = attentionFriendsSec;
				   break;
				   
			   case Task.TASK_RECMMEND_CURR_APP: //发表推荐信
			     try {
					  Status ststus= weibo.updateStatus((String)task.getTaskParam().get("msg"));
					  message.obj="微博发表成功";
				  } catch (Exception e) {
					  e.printStackTrace();
					  message.obj="微博发表失败";
				  } 
				   break;
				
			   case Task.TASK_FLLOW_AUTHOR:   //关注作者
					
				try {
					 message.obj = weibo.createFriendship("1967935164");// 恢复关注用户
				} catch (Exception e) {
					 message.obj = null;
					e.printStackTrace();
				}
					break;   
				   
			}
			message.what = task.getTaskID();

		} catch (WeiboException e) {
			message.what = e.getStatusCode();
		} catch (Exception es) {
			message.what = this.weiboerror;
		}

		allTask.remove(task);// 完成任务
		// 发送刷新 UI的消息到主线程
		mHandler.sendMessage(message);
	}

	// 刷新UI
	public final Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Task.TASK_USER_LOGIN: 
				if(!MainService.isAutoLogin){
					MainService.getActivityByName("LoginActivity")
					.refresh(LoginActivity.REF_LOGIN_RESULT, msg.obj);
				}
				break;
				
		    //切换登陆用户		
			case Task.TASK_MODIFY_USER_LOGIN: 
					MainService.getActivityByName("AddCountctivity")
					.refresh(null,  msg.obj);
				break;
				
			case Task.TASK_SAVE_LOIGINING_USER_BASE_INFO:
				MainService.getActivityByName("MyListActivity")
				  .refresh(MyListActivity.REF_GET_CURRENT_USET_NAME,  msg.obj);
				break;
				
			case Task.TASK_GET_USER_INFO: //获取用户详细信息
				////System.out.println("----TASK_GET_USER_INFO-----");
				MainService.getActivityByName("UserInfoActivity")
				  .refresh(UserInfoActivity.REFRESH,  msg.obj);
				break;
				
			case Task.TASK_GET_USER_HOMETIMEINLINE:// 更新Weibo首页
				////System.out.println("---TASK_GET_USER_HOMETIMEINLINE------");

				MainService.getActivityByName("MyListActivity")
				     .refresh(MyListActivity.REF_WEIBO, msg.obj);
				
				break;
				
			case Task.TASK_GET_USER_HOMETIMEINLINE_REFRESH:// 更新Weibo首页
				////System.out.println("---TASK_GET_USER_HOMETIMEINLINE_REFRESH------");
				MainService.getActivityByName("MyListActivity")
				.refresh(MyListActivity.REF_WEIBO, msg.obj);
				
				break;
 
			case Task.TASK_GET_USER_HOMETIMEINLINE_FROM_LOCAL:// 从本地更新Weibo首页

				MainService.getActivityByName("MyListActivity")
				   .refresh(MyListActivity.REF_WEIBO, msg.obj);
				break;

			case Task.TASK_GET_USER_HOMETIMEINLINE_MORE:// 更新Weibo首页更多
				MainService.getActivityByName("MyListActivity")
				       .refresh(MyListActivity.REF_WEIBO_MORE, msg.obj);

				break;
	//-----------------------------------------------------	
			//查看微博信息的评论列表
			case Task.TASK_GET_WEIBO_COMMENT_LIST:
				MainService.getActivityByName("ListCommentsActivity")
			       .refresh(null, msg.obj);
				break;
				
			case Task.TASK_NEW_WEIBO_COMMENT://发表评论
				 MainService.getActivityByName("CommentActivity")
				 .refresh(null ,msg.obj);
//				 .refresh(CommentActivity.REF_NEW_WEIBO_COMM_RESULT,msg.obj);
			     break; 
			     
			case Task.TASK_REPOST_WEIBO://转发微博
				MainService.getActivityByName("RepostBlogActivity")
				.refresh(null ,msg.obj);
//				 .refresh(CommentActivity.REF_NEW_WEIBO_COMM_RESULT,msg.obj);
				break;  
				
			case Task.TASK_REFRESH_WEIBO://刷新微博
				MainService.getActivityByName("ViewWeiboActivity")
				.refresh(ViewWeiboActivity.REFRESH ,msg.obj);
//				 .refresh(CommentActivity.REF_NEW_WEIBO_COMM_RESULT,msg.obj);
				break;    	
				
			case Task.TASK_FAV_WEIBO://收藏微博
				MainService.getActivityByName("ViewWeiboActivity")
				.refresh(ViewWeiboActivity.FAV ,msg.obj);
				break;    		 
				
				
 //--------------------------推荐信息------------------------------------
		    //获取推荐信息
			case Task.TASK_GET_USER_RECOMMEND_INFO_HOME_INLINE:
				MainService.getActivityByName("ListRcemmendInfoActivity")
			     .refresh(ListRcemmendInfoActivity.REF_WEIBO, msg.obj);
				break; 
		    
			case Task.TASK_GET_USER_RECOMMEND_INFO_HOME_INLINE_MORE:
				MainService.getActivityByName("ListRcemmendInfoActivity")
			     .refresh(ListRcemmendInfoActivity.REF_WEIBO_MORE, msg.obj);
				break; 
				
				//获取推荐信息
			case Task.TASK_GET_USER_RECOMMEND_INFO_HOME_INLINE_REFRESH:
				MainService.getActivityByName("ListRcemmendInfoActivity")
				.refresh(ListRcemmendInfoActivity.REF_WEIBO, msg.obj);
				break; 
				
				
				
	//-------------------------------用户头像----------------------	
//				ddd
				////更新用户ICON
			case Task.TASK_GET_USER_IMAGE_ICON:
				 MainService.getActivityByName("MyListActivity")
			           . refresh(MyListActivity.REF_ICON, msg.obj);
			     break;
			  
			 	////更新用户ICON -->信息推荐
			case Task.TASK_GET_USER_IMAGE_ICON_FOR_RECOMMOND_STATUS:
				
					MainService.getActivityByName("ListRcemmendInfoActivity")
					. refresh(ListRcemmendInfoActivity.REF_ICON, msg.obj);
				 
				break;
				
				
			 //提到我的微博新-->用户ICON 
			case Task.TASK_GET_USER_IMAGE_ICON_FOR_METTION_ME:
				if(MessageActivity.CHANGE_FLAG == 0){
					MainService.getActivityByName("MessageActivity")
					. refresh(MessageActivity.REF_GET_WEIBO_ATME_USER_ICON, msg.obj);
				}
				break;
				
			//评论我的微博的用户ICON
			case Task.TASK_GET_COMM_USERT_ICON:
				if(MessageActivity.CHANGE_FLAG ==  1){
					MainService.getActivityByName("MessageActivity")
					. refresh(MessageActivity.REF_GET_WEIBO_COMM_USER_ICON, msg.obj);
				}
				break;
				
				   //获取发私信给我的好友的头像	   
			 case Task.TASK_GET_DIRECT_MSG_USERT_ICON:
				 if(MessageActivity.CHANGE_FLAG == 2){
					 MainService.getActivityByName("MessageActivity")
					 . refresh(MessageActivity.REF_GET_WEIBO_MESSAGE_USER_ICON, msg.obj);
				 }
				 break;
			     
			     
			     //获取 我关注的好友的头像	   
			 case Task.TASK_GET_USER_IMAGE_ICON_FAVORITE:
				 MainService.getActivityByName("ListMyFavoriteActivity")
				 . refresh(ListMyFavoriteActivity.REF_ICON, msg.obj);
				 break;
				 
			 case Task.TASK_GET_USER_ICON_4_USERINFO:
				 MainService.getActivityByName("UserInfoActivity")
				 . refresh(UserInfoActivity.REFRESH_USERICON, msg.obj);
				 break;
 
			     
			     
			     
//---------------------------加载微博中的图片------------------------------------
			case Task.TASK_GET_WEIBO_IMAGE: 
				MainService.getActivityByName("MyListActivity")
				  .refresh(MyListActivity.REF_GET_WEIBO_IMAGE, msg.obj);
			    break;	
			    
			//刷新提到我的微博信息中的图片信息   
			case Task.TASK_GET_USER_WEIBO_IMAGE_FOR_METTION_ME: 
				MainService.getActivityByName("MessageActivity")
				.refresh(MessageActivity.REF_GET_WEIBO_MESSAGE_USER_ICON, msg.obj);
				break;	
			//刷新推荐微博信息中的图片信息	
			case Task.TASK_GET_USER_WEIBO_IMAGE_FOR_RECOMMOND_STATUS: 
				MainService.getActivityByName("ListRcemmendInfoActivity")
				.refresh(ListRcemmendInfoActivity.REF_GET_WEIBO_IMAGE, msg.obj);
				break;	
			
		    //刷新收藏夹中微博信息的图片信息		
			case Task.TASK_GET_USER_WEIBO_IMAGE_FOR_FAV_STATUS: 
				MainService.getActivityByName("ListMyFavoriteActivity")
				.refresh(ListMyFavoriteActivity.REF_GET_WEIBO_IMAGE, msg.obj);
				break;	 
				
			//刷新微博信息的图片信息  ---->ViewWeiboActivity		
			case Task.TASK_GET_WEIBO_IMAGE_FOR_VIEW_WEIBO_INFO: 
				MainService.getActivityByName("ViewWeiboActivity")
				.refresh(ViewWeiboActivity.REF_ICON_OR_WEIBO_IMAGE, msg.obj);
				break;	
 
		    //耍收藏夹的微博信息
			case Task.TASK_GET_USER_FAVORITE:
				MainService.getActivityByName("ListMyFavoriteActivity")
				.refresh(ListMyFavoriteActivity.REF_WEIBO, msg.obj);
				break;	
				
				
				
				
			 case Task.TASK_NEW_WEIBO://发表微博
					////System.out.println("----TASK_NEW_WEIBO-----");
					 IWeiboActivity ha3 = MainService.getActivityByName("NewWeiboActivity");
				     ha3.refresh(NewWeiboActivity.REF_NEW_WEIBO_RESULT,msg.obj);
				     break;
				     
			case Task.TASK_NEW_WEIBO_PIC://发表图片微博信息
				 MainService.getActivityByName("NewWeiboActivity")
				            .refresh(NewWeiboActivity.REF_NEW_WEIBO_RESULT, msg.obj); 
			       break;
			  
			case Task.TASK_NEW_WEIBO_GPS://发表GPS微博信息
			    MainService.getActivityByName("NewWeiboActivity")
	                       .refresh(NewWeiboActivity.REF_NEW_WEIBO_RESULT, msg.obj);  
			      break;
			      
			 //发表反馈信息      
			case Task.TASK_NEW_SUGGESTION_TO_AUTHOR_WEIBO:
				MainService.getActivityByName("SendSuggestionToAuthorActivity")
				.refresh(SendSuggestionToAuthorActivity.REF_NEW_WEIBO_RESULT,  msg.obj);  
				break;
			  
			      
			      
		//-------------------------Message----------------------------	      
		   case Task.TASK_GET_METION_ME:    //获取提到我的所有所有微博信息
			   if(MessageActivity.CHANGE_FLAG == 0){
				   MainService.getActivityByName("MessageActivity")
					 .refresh(MessageActivity.REF_GET_WEIBO_ATME,  msg.obj);
				   msg.obj = null;
			   }
			
			   break; 
			   
			case Task.TASK_GET_WEIBO_COMMENT://当前用户发表的所有评论
				 if(MessageActivity.CHANGE_FLAG == 1){
					MainService.getActivityByName("MessageActivity")
					 .refresh(MessageActivity.REF_GET_WEIBO_COMM, msg.obj);
					msg.obj = null;
				 }
			
				break;
				
			case Task.TASK_GET_WEIBO_MESSAGE://当前用户的私信
				 if(MessageActivity.CHANGE_FLAG == 2){
				    MainService.getActivityByName("MessageActivity")
					 .refresh(MessageActivity.REF_GET_WEIBO_MESSAGE, msg.obj);
					msg.obj = null;
				 }
				break;      
	   
				
			case Task.TASK_DEL_ACCOUNT:
				MainService.getActivityByName("AccountManagerActivity")
				 .refresh(null, msg.obj);
				break;
				
				
				// ---------------------罗洪州---------------------

			case Task.TASK_GET_WEIBO_USER:// 更新我的Weibo首页
				////System.out.println("---TASK_GET_WEIBO_USER------");
				IWeiboActivity haha = MainService
						.getActivityByName("ListMyWeibosActivity");
				haha.refresh(ListMyWeibosActivity.REF_MYWEIBO, msg.obj);
				break;
			case Task.TASK_GET_MORE_WEIBO_USER:// 更新Weibo首页更多
				////System.out.println("---TASK_GET_MORE_WEIBO_USER------");
				IWeiboActivity myhamore = MainService
						.getActivityByName("ListMyWeibosActivity");
				myhamore.refresh(ListMyWeibosActivity.REF_MYWEIBO_MORE, msg.obj);
				break;

			case Task.TASK_GET_MY_TOP:


				break;
			case Task.TASK_GET_MY_TOP_MORE:

				break;
			
			 case Task.TAST_GET_FANS_INFO: //获取粉丝: 
					////System.out.println("----//获取粉丝:-----");
					MainService.getActivityByName("ListMyFansActivity")
					.refresh(ListMyFansActivity.REF_FANS,msg.obj);
					break;
					
			 case Task.TAST_GET_MORE_FANS_INFO: //获取粉丝
				////System.out.println("----//获取粉丝:-----");
				MainService.getActivityByName("ListMyFansActivity")
				.refresh(ListMyFansActivity.REF_MORW_FANS  ,msg.obj);
				break;
				
			 case Task.TASK_GET_USER_IMAGE_ICON_FANS: 
				 ////System.out.println("----//获取粉丝:-----");
				 MainService.getActivityByName("ListMyFansActivity")
				 .refresh(ListMyFansActivity.REF_ICON  ,msg.obj);
				 break;
				 
			case Task.TAST_GET_ATTENTION_INFO:
				////System.out.println("--获取好像有的关注信息 -TAST_GET_ATTENTION_INFO-");
				MainService.getActivityByName("ListAllFriendsActivity").refresh(ListAllFriendsActivity.REF_ATTENTION, msg.obj);
				break;
			case Task.TAST_GET_MORE_ATTENTION_INFO : //获取更多的关注信息
				////System.out.println("--获取好像有的关注信息 -TAST_GET_ATTENTION_INFO-");
				MainService.getActivityByName("ListAllFriendsActivity").refresh(ListAllFriendsActivity.REF_MORE_ATTENTION, msg.obj);
				break;	
		     
				//树朾添加------------------------------------------------------------------
				/*
				* 添加取消关注任务
				*/
						
			case Task.TASK_ATTEND_CANCEL: //取消关注
					////System.out.println("---TASK_ATTEND_CANCEL------");
					
					////System.out.println("执行取消关注任务完毕");
					
					MainService.getActivityByName("ListAllFriendsActivity")
							.refresh(ListAllFriendsActivity.REF_ATTEND_CANCEL_FINISH, msg.obj);
					break;

					/*
					* 添加恢复关注任务
					*/
							
			case Task.TASK_ATTEND_RESTORE: //恢复关注
					////System.out.println("---TASK_ATTEND_RESTORE------");
					
					////System.out.println("执行恢复关注任务完毕");
					
					MainService.getActivityByName("ListAllFriendsActivity")
							.refresh(ListAllFriendsActivity.REF_ATTEND_RESTORE_FINISH, msg.obj);
					break;
					
			case Task.TASK_FANS_CANCEL: //取消关注
				////System.out.println("---TASK_FANS_RESTORE------");
				
				////System.out.println("执行取消关注任务完毕-----");
				
				MainService.getActivityByName("ListMyFansActivity")
						.refresh(ListMyFansActivity.REF_FANS_CANCEL_FINISH, msg.obj);
				break;

				/*
				* 添加恢复关注任务
				*/
						
		case Task.TASK_FANS_RESTORE: //恢复关注
				////System.out.println("---TASK_FANS_RESTORE------");
				
				////System.out.println("执行恢复关注任务完毕-0---");
				
				MainService.getActivityByName("ListMyFansActivity")
						.refresh(ListMyFansActivity.REF_FANS_RESTORE_FINISH, msg.obj);
				break;
				
		case Task.TASK_RECMMEND_CURR_APP: //推荐
			
			   MainService.getActivityByName("RecommendCurrAppActivity")
			            .refresh(RecommendCurrAppActivity.REF_NEW_WEIBO_RESULT, msg.obj);
	           break;  
			
		case Task.TASK_FLLOW_AUTHOR: //关注
			
			MainService.getActivityByName("AboutActivity")
			    .refresh(AboutActivity.REF_FOLLOW_RESULT, msg.obj);
			break;  
			
		 }

		}

	};

	// 提示用户网络异常
	public static void alertNetError(final Activity context) {
		final AlertDialog.Builder ab = new AlertDialog.Builder(context);
		// 设定标题
		ab.setTitle(context.getResources().getString(R.string.net_err_title));
		// 设定标题
		ab.setMessage(context.getResources().getString(R.string.net_err_info));
		// 设定退出按钮
		ab.setNegativeButton(context.getResources()
				.getString(R.string.exit_app), new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				exitApp(context);
			}
		});
		
		// 网络设置按钮
		ab.setPositiveButton(
				context.getResources().getString(R.string.net_setting),
				new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						context.startActivityForResult(
								new Intent(
										android.provider.Settings.ACTION_WIRELESS_SETTINGS),
								0);
					}
				});
		ab.create().show();
	}

	// 退出应用程序
	public static void exitApp(Activity context) {// 退出所有Activity
		final ArrayList<IWeiboActivity> allActivities = allActivity;
		for (int i = 0, length = allActivities.size(); i < length; i++) {
			((Activity) allActivity.get(i)).finish();
		}
		allActivity.clear();
		// 退出Service
//		context.stopService(new Intent("com.jm.weibo.logic.MainService"));
		context.stopService(new Intent("com.itita.weibo.manager.MainService"));
		// 关闭子线程
		MainService.isrun = false;
		// 关闭广播接口
		//MainService.mainService.unregisterReceiver(MainService.mainService.netReceiver);
		 
		//完全关闭应用程序
		int sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK); 
		try {
			 
			ActivityManager manager = (ActivityManager)MainService.mainService.getSystemService(ACTIVITY_SERVICE);    
			manager.restartPackage(MainService.mainService.getPackageName());  
	 
			ActivityManager am = (ActivityManager) MainService.mainService.getSystemService(ACTIVITY_SERVICE); 
			am.killBackgroundProcesses("com.itita.weibo.ui");
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
//		if(sdkVersion <=  9){
//        	ActivityManager manager = (ActivityManager)MainService.mainService.getSystemService(ACTIVITY_SERVICE);    
//        	manager.restartPackage(MainService.mainService.getPackageName());  
//        }else{
//        	ActivityManager am = (ActivityManager) MainService.mainService.getSystemService(ACTIVITY_SERVICE); 
//        	am.killBackgroundProcesses("com.itita.weibo.ui");
//        }
        
//		////System.out.println(sdkVersion);
//        ////System.out.println(sdkVersion);
//        ////System.out.println(sdkVersion);
//        ////System.out.println(sdkVersion);

	}

	// 添加任务
	public static void addNewTask(Task ts) {
		allTask.add(ts);
	}
   
	public static void addActivity(IWeiboActivity activity){
		synchronized (allActivity) { 
			allActivity.add(activity);
		}
		
	}
	// 提示是否退出应用程序
	public static void promptExitApp(final Activity context) {
		// 创建对话框
		final AlertDialog.Builder ab = new AlertDialog.Builder(context);
		final LayoutInflater li = LayoutInflater.from(context);
		final View msgview = li.inflate(R.layout.exitdialog, null);
		
		ab.setView(msgview);// 设定对话框显示的内容
		ab.setIcon(R.drawable.ic_help);
		ab.setPositiveButton(R.string.app_exit_ok, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				arg0.dismiss();
				exitApp(context);// 退出整个应用
			}
		});
		ab.setNegativeButton(R.string.app_exit_cancle, null);
		ab.show();
	}
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

	public void onDestroy() {
		super.onDestroy();
	}
    
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	/**
	 * 从线程池获取线程
	 * @return 任务线程 
	 */
//	public TaskThread getThreadInstance(){
//        if(taskThreads.size() == 0){ //线程池中没有可用的线程
//        	taskThreads.add(new TaskThread(null));
//        }
//		return taskThreads.get(0);
//	}

}
